package com.grieous.solr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import domain.Item;
import domain.PageModel;

public class SearchFile {

	@SuppressWarnings("static-access")
	public static PageModel search(String searchText, Integer page,
			Integer pageSize) {
		HttpSolrServer server = SolrServerManager.getInstance()
				.getHttpSolrServer();
		String baseURL=SolrServerManager.getUrl();
		server.setBaseURL(baseURL + "core2");
		SolrQuery query = new SolrQuery();
		query.setQuery("tx:" + searchText);
		// 设置分页
		page = page == null ? 1 : page;
		pageSize = pageSize == null ? 10 : pageSize;
		query.setStart((page - 1) * pageSize).setRows(pageSize);
		// 设置高亮
		query.setHighlight(true).addHighlightField("title")
				.addHighlightField("text").setHighlightSimplePre("<em>")
				.setHighlightSimplePost("</em>").setHighlightSnippets(2)
				.setHighlightFragsize(1000);
		try {
			QueryResponse queryResponse = server.query(query);
			server.setBaseURL(baseURL);
			SolrDocumentList list = queryResponse.getResults();
			Integer counts = (int) list.getNumFound();
			if (counts > 0) {
				// 获取所有高亮的字段
				Map<String, Map<String, List<String>>> highlightMap = queryResponse
						.getHighlighting();
				Item item = null;
				String text = "";
				String title = "";
				String path = "";
				List<Item> itemList = new ArrayList<Item>();
				for (SolrDocument sd : list) {
					if (sd.getFieldValue("path") == null) {
						continue;
					}
					path = sd.getFieldValue("path").toString();
					if (sd.getFieldValue("title") != null) {
						title = sd.getFieldValue("title").toString();
					}
					if (sd.getFieldValue("text") != null) {
						text = sd.getFieldValue("text").toString();
					}
					List<String> textList = null;
					List<String> titleList = null;
					if (highlightMap.get(path) != null) {
						textList = highlightMap.get(path).get("text");
						titleList = highlightMap.get(path).get("title");
						if (titleList != null && titleList.size() > 0) {
							title = titleList.get(0);
						}
						if (textList != null && textList.size() > 0) {
							text = textList.get(0);
						}
					}

					item = new Item();
					item.setTitle(title);
					item.setText(text);
					item.setPath(path);
					itemList.add(item);
				}
				PageModel pageModel = new PageModel();
				pageModel.setList(itemList);
				pageModel.setCurrentPage(page);
				pageModel.setPageSize(pageSize);
				pageModel.setTotalRecord(counts);
				pageModel.setTotalPage((counts - 1) / pageSize);
				return pageModel;
			}
		} catch (SolrServerException e) {
			e.printStackTrace();
		}
		return null;
	}

}
